﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Text.RegularExpressions;
using System.Windows.Forms;
using Assist;
using Autodesk.Revit.DB;
using Autodesk.Revit.DB.Plumbing;
using YArchitech.Controls;
using YArchitech.DataModel;
using YArchitech.LIB;
using YArchitech.LIB.YJKControls;
using YArchitech.Revit;

namespace YArchitech.Plumbing
{
	public partial class FormCreateHeatingPipe : HYBaseForm
	{
		public FormCreateHeatingPipe(Autodesk.Revit.DB.Document doc, List<string> levelUp)
		{
			this.InitializeComponent();
			new YJKProcessTools().ShowInTaskbarChange(this);
			new List<KeyValuePair<System.Windows.Forms.Control, bool>>();
			this.doc = doc;
			this.levelUp = levelUp;
			base.Icon = Resource.YArchitechIcon;
			this.osf = new OperateSegmentFitting(doc);
		}

		private void FormCreateThwartwisePipe_Load(object sender, EventArgs e)
		{
			this.hyGBBSParameterSetting.Anchor = AnchorStyles.Bottom;
			this.systemType = new HYHeatingPipeSystemType(this.doc);
			this.systemType.Flag = true;
			this.systemType.Location = new System.Drawing.Point(12, 15);
			this.systemType.SystemTypeChanged += this.hySystemType_SystemTypeChanged;
			this.systemType.btnPipeSettingChanged += this.hySystemType_btnPipeSettingChanged;
			this.hyGBBSSystemType.Controls.Add(this.systemType);
			foreach (string item in this.levelUp)
			{
				this.comboBoxLevel.Items.Add(item);
			}
			double num = 1.0;
			double num2 = 1.0;
			WinDPIManager.GetWinFormXYDpi(ref num, ref num2);
			this.hyGBBSSystemType.Height = this.hyGBBSSystemType.Height - Convert.ToInt32((double)this.systemType.HeightLess * num2);
			base.Height -= Convert.ToInt32((double)this.systemType.HeightLess * num2);
			this.GetUserData();
		}

		private void hySystemType_SystemTypeChanged(object sender, EventArgs e)
		{
			this.comboBoxPipeDiameter.Items.Clear();
			if (this.systemType.currentSystemTypeInfo != null)
			{
				foreach (MEPSize mepsize in this.osf.GetPipeSegmentByPipeTypeName(this.systemType.currentSystemTypeInfo.PipeType).GetSizes())
				{
					this.comboBoxPipeDiameter.Items.Add(Math.Round(304.8 * mepsize.NominalDiameter, MidpointRounding.AwayFromZero).ToString());
				}
				this.comboBoxPipeDiameter.SelectedIndex = 0;
			}
		}

		private void hySystemType_btnPipeSettingChanged(object sender, EventArgs e)
		{
			this.systemType.SaveSystemType();
			this.hyGBBSSystemType.Controls.Remove(this.systemType);
			this.systemType = new HYHeatingPipeSystemType(this.doc);
			this.systemType.Flag = true;
			this.systemType.Location = new System.Drawing.Point(12, 15);
			this.systemType.SystemTypeChanged += this.hySystemType_SystemTypeChanged;
			this.systemType.btnPipeSettingChanged += this.hySystemType_btnPipeSettingChanged;
			this.hyGBBSSystemType.Controls.Add(this.systemType);
		}

		private void GetUserData()
		{
			string[] heatingPipeText = this.userSetting.GetHeatingPipeText();
			this.comboBoxPipeDiameter.Items.Clear();
			if (this.systemType.currentSystemTypeInfo != null)
			{
				foreach (MEPSize mepsize in this.osf.GetPipeSegmentByPipeTypeName(this.systemType.currentSystemTypeInfo.PipeType).GetSizes())
				{
					this.comboBoxPipeDiameter.Items.Add(Math.Round(304.8 * mepsize.NominalDiameter, MidpointRounding.AwayFromZero).ToString());
				}
				this.comboBoxPipeDiameter.Text = heatingPipeText[0];
			}
			if (string.IsNullOrEmpty(this.comboBoxPipeDiameter.Text) && this.comboBoxPipeDiameter.Items != null && this.comboBoxPipeDiameter.Items.Count > 0)
			{
				this.comboBoxPipeDiameter.SelectedItem = this.comboBoxPipeDiameter.Items[0];
				heatingPipeText[0] = this.comboBoxPipeDiameter.Text;
			}
			if (string.IsNullOrEmpty(heatingPipeText[1]))
			{
				this.textBoxLevel.Text = "0.0";
				heatingPipeText[1] = "0.0";
			}
			else
			{
				string[] array = heatingPipeText[1].Split(new char[]
				{
					','
				});
				this.textBoxLevel.Items.AddRange(array);
				this.textBoxLevel.Text = array[0];
				this.textLevel = this.textBoxLevel.Text;
			}
			if (string.IsNullOrEmpty(heatingPipeText[2]))
			{
				this.textBoxOffset.Text = "0.0";
				heatingPipeText[2] = "0.0";
			}
			else
			{
				string[] array2 = heatingPipeText[2].Split(new char[]
				{
					','
				});
				this.textBoxOffset.Items.AddRange(array2);
				this.textBoxOffset.Text = array2[0];
				this.textOffset = this.textBoxOffset.Text;
			}
			if (string.IsNullOrEmpty(heatingPipeText[3]))
			{
				this.hyTextBox1.Text = "0.0";
				heatingPipeText[3] = "0.0";
			}
			else
			{
				string[] array3 = heatingPipeText[3].Split(new char[]
				{
					','
				});
				this.hyTextBox1.Items.AddRange(array3);
				this.hyTextBox1.Text = array3[0];
			}
			this.textDiameter = heatingPipeText[0];
		}

		private void SetUserData()
		{
			string[] texts = new string[]
			{
				this.comboBoxPipeDiameter.Text,
				this.GetUserData(this.textBoxLevel),
				this.GetUserData(this.textBoxOffset),
				this.GetUserData(this.hyTextBox1)
			};
			this.userSetting.WriteHeatingPipeText(texts);
		}

		private string GetUserData(System.Windows.Forms.ComboBox combobox)
		{
			if (combobox.Items.Contains(combobox.Text))
			{
				string text = combobox.Text;
				string text2 = combobox.Text;
				combobox.Items.Remove(text);
				foreach (object obj in combobox.Items)
				{
					text = text + "," + obj.ToString();
				}
				combobox.Items.Add(text2);
				combobox.Text = text2;
				return text;
			}
			List<string> list = new List<string>();
			foreach (object obj2 in combobox.Items)
			{
				list.Add(obj2.ToString());
			}
			if (list.Count == 10)
			{
				list.Remove(list.Last<string>());
			}
			string text3 = combobox.Text;
			foreach (string str in list)
			{
				text3 = text3 + "," + str;
			}
			return text3;
		}

		private double zCoord(double offset)
		{
			return offset / 0.3048;
		}

		public PipeType Get_PipeType(Autodesk.Revit.DB.Document document, string pipeTypeName)
		{
			ICollection<Element> elementByClass = YJKElementFilter.GetElementByClass(typeof(PipeType), document);
			return this.GetElementSameName(elementByClass, pipeTypeName) as PipeType;
		}

		public PipingSystemType Get_PipingSystemType(Autodesk.Revit.DB.Document document, string pipingSystemTypeName)
		{
			ICollection<Element> elementByClass = YJKElementFilter.GetElementByClass(typeof(PipingSystemType), document);
			return this.GetElementSameName(elementByClass, pipingSystemTypeName) as PipingSystemType;
		}

		public Level Get_Level(Autodesk.Revit.DB.Document document)
		{
			Level level = document.ActiveView.GenLevel;
			if (level == null)
			{
				FilteredElementCollector filteredElementCollector = new FilteredElementCollector(this.doc);
				filteredElementCollector.OfClass(typeof(Level)).OfCategory(BuiltInCategory.OST_Levels);
				double num = 10000.0;
				foreach (Element element in filteredElementCollector)
				{
					Level level2 = (Level)element;
					if (level2.ProjectElevation < num)
					{
						level = level2;
						num = level2.ProjectElevation;
					}
				}
			}
			return level;
		}

		private Element GetElementSameName(ICollection<Element> collection, string elemName)
		{
			Element result = null;
			foreach (Element element in collection)
			{
				if (element != null && element.Name == elemName)
				{
					result = element;
				}
			}
			return result;
		}

		private void buttonOK_Click(object sender, EventArgs e)
		{
			if (this.textBoxLevel.Text != "" && this.textBoxOffset.Text != "" && this.comboBoxPipeDiameter.Text != "" && this.hyTextBox1.Text != "")
			{
				double num = double.Parse(this.textBoxLevel.Text) * 1000.0 + this.Get_Level(this.doc).ProjectElevation * 304.8;
				double offset = double.Parse(this.textBoxOffset.Text);
				double num2 = double.Parse(this.hyTextBox1.Text);
				this.level = num;
				this.Offset = offset;
				this.slope = num2;
				this.diameter = this.comboBoxPipeDiameter.Text;
				this.SetUserData();
				base.DialogResult = DialogResult.OK;
				return;
			}
			MessageBox.Show("偏移量,直径与相对标高都不能为空", "盈建科提示", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
		}

		private void buttonESC_Click(object sender, EventArgs e)
		{
			base.DialogResult = DialogResult.Cancel;
		}

		private void FormCreateThwartwisePipe_Deactivate(object sender, EventArgs e)
		{
			if (!Regex.IsMatch(this.comboBoxPipeDiameter.Text, "^[+-]?[0-9]+\\d*[.]?\\d*$"))
			{
				MessageBox.Show("直径必须为有效数字", "盈建科提示", MessageBoxButtons.OK, MessageBoxIcon.Hand);
				this.comboBoxPipeDiameter.Focus();
				return;
			}
			if (!Regex.IsMatch(this.textBoxOffset.Text, "^[+-]?[0-9]+\\d*[.]?\\d*$"))
			{
				MessageBox.Show("水平偏移必须为有效数字", "盈建科提示", MessageBoxButtons.OK, MessageBoxIcon.Hand);
				this.textBoxOffset.Text = "0";
				this.textBoxOffset.Focus();
				this.textBoxOffset.SelectAll();
				return;
			}
			if (!Regex.IsMatch(this.textBoxLevel.Text, "^[+-]?[0-9]+\\d*[.]?\\d*$"))
			{
				MessageBox.Show("相对标高必须为有效数字", "盈建科提示", MessageBoxButtons.OK, MessageBoxIcon.Hand);
				this.textBoxLevel.Text = "0";
				this.textBoxLevel.Focus();
				this.textBoxLevel.SelectAll();
				return;
			}
			if (this.isDra && !Regex.IsMatch(this.hyTextBox1.Text, "^[+-]?[0-9]+\\d*[.]?\\d*$"))
			{
				MessageBox.Show("坡度必须为有效数字", "盈建科提示", MessageBoxButtons.OK, MessageBoxIcon.Hand);
				this.hyTextBox1.Text = "0";
				this.hyTextBox1.Focus();
				this.hyTextBox1.SelectAll();
				return;
			}
			if (!(this.textBoxLevel.Text != "") || !(this.textBoxOffset.Text != "") || !(this.comboBoxPipeDiameter.Text != "") || !(this.hyTextBox1.Text != ""))
			{
				MessageBox.Show("偏移量,直径与相对标高都不能为空", "盈建科提示", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
				if (this.textBoxLevel.Text == "")
				{
					this.textBoxLevel.Text = this.textLevel;
					this.textBoxLevel.Focus();
				}
				if (this.comboBoxPipeDiameter.Text == "")
				{
					this.comboBoxPipeDiameter.Text = this.textDiameter;
					this.comboBoxPipeDiameter.Focus();
				}
				if (this.textBoxOffset.Text == "")
				{
					this.textBoxOffset.Text = this.textOffset;
					this.textBoxOffset.Focus();
				}
				return;
			}
			if (Math.Abs(double.Parse(this.textBoxOffset.Text.Trim())) > 50000.0)
			{
				MessageBox.Show("水平偏移的绝对值不能超过50000mm", "盈建科提示", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
				this.textBoxOffset.Focus();
				this.textBoxOffset.Text = this.textOffset;
			}
			if (Math.Abs(double.Parse(this.textBoxLevel.Text.Trim())) > 50.0)
			{
				MessageBox.Show("相对标高的绝对值不能超过50m", "盈建科提示", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
				this.textBoxLevel.Focus();
				this.textBoxLevel.Text = this.textLevel;
				return;
			}
			double num = double.Parse(this.textBoxLevel.Text) * 1000.0 + this.Get_Level(this.doc).ProjectElevation * 304.8;
			double offset = double.Parse(this.textBoxOffset.Text);
			double num2 = double.Parse(this.hyTextBox1.Text);
			this.level = num;
			this.Offset = offset;
			if (this.isDra)
			{
				this.slope = num2;
			}
			else
			{
				this.slope = 0.0;
			}
			this.diameter = this.comboBoxPipeDiameter.Text;
			this.textLevel = this.textBoxLevel.Text.Trim();
			this.textOffset = this.textBoxOffset.Text.Trim();
			this.textDiameter = this.comboBoxPipeDiameter.Text.Trim();
		}

		[DllImport("user32.dll")]
		public static extern void keybd_event(byte bVk, byte bScan, int dwFlags, int dwExtraInfo);

		[DllImport("user32.dll", EntryPoint = "SendMessageA")]
		public static extern int SendMessage(IntPtr hwnd, int wMsg, IntPtr wParam, IntPtr lParam);

		private void FormCreateThwartwisePipe_FormClosed(object sender, FormClosedEventArgs e)
		{
			int bVk = 27;
			int dwFlags = 0;
			int dwFlags2 = 2;
			int wMsg = 7;
			FormCreateHeatingPipe.SendMessage(HeatingPipeCreate.hWndRevit.Handle, wMsg, IntPtr.Zero, IntPtr.Zero);
			FormCreateHeatingPipe.keybd_event((byte)bVk, 0, dwFlags, 0);
			FormCreateHeatingPipe.keybd_event((byte)bVk, 0, dwFlags2, 0);
		}

		private void FormCreateThwartwisePipe_Leave(object sender, EventArgs e)
		{
			if (!base.ClientRectangle.Contains(Cursor.Position))
			{
				int wMsg = 7;
				FormCreateHeatingPipe.SendMessage(HeatingPipeCreate.hWndRevit.Handle, wMsg, IntPtr.Zero, IntPtr.Zero);
				return;
			}
			base.Focus();
		}

		private void FormCreateThwartwisePipe_MouseLeave(object sender, EventArgs e)
		{
			if (!base.ClientRectangle.Contains(Cursor.Position))
			{
				int wMsg = 7;
				FormCreateHeatingPipe.SendMessage(HeatingPipeCreate.hWndRevit.Handle, wMsg, IntPtr.Zero, IntPtr.Zero);
				return;
			}
			base.Focus();
		}

		private void FormCreateThwartwisePipe_KeyDown(object sender, KeyEventArgs e)
		{
			if (e.KeyCode == Keys.Escape)
			{
				base.Close();
			}
		}

		private void FormCreateThwartwisePipe_FormClosing(object sender, FormClosingEventArgs e)
		{
			this.systemType.SaveSystemType();
			this.SetUserData();
		}

		private void textBoxLevel_KeyPress(object sender, KeyPressEventArgs e)
		{
			if (((e.KeyChar < '-' || e.KeyChar > '9') && e.KeyChar != '\b') || e.KeyChar == '/')
			{
				e.Handled = true;
			}
		}

		private bool IsItemsContainsStr(System.Windows.Forms.ComboBox.ObjectCollection items, string str, ref string dupStr)
		{
			foreach (object obj in items)
			{
				if (Math.Abs(obj.ToString().ToDouble(0.0) - str.ToDouble(0.0)) < 0.0001)
				{
					dupStr = obj.ToString();
					return true;
				}
			}
			return false;
		}

		private void textBoxLevel_DropDown(object sender, EventArgs e)
		{
			string text = this.textBoxLevel.Text;
			string value = "";
			if (!this.IsItemsContainsStr(this.textBoxLevel.Items, text, ref value))
			{
				List<string> list = new List<string>();
				foreach (object obj in this.textBoxLevel.Items)
				{
					list.Add(obj.ToString());
				}
				if (list.Count == 10)
				{
					list.Remove(list.Last<string>());
				}
				this.textBoxLevel.Items.Clear();
				this.textBoxLevel.Items.Add(text);
				this.textBoxLevel.Items.AddRange(list.ToArray());
				return;
			}
			this.textBoxLevel.Items.Remove(value);
			List<string> list2 = new List<string>();
			foreach (object obj2 in this.textBoxLevel.Items)
			{
				list2.Add(obj2.ToString());
			}
			this.textBoxLevel.Items.Clear();
			this.textBoxLevel.Items.Add(text);
			this.textBoxLevel.Items.AddRange(list2.ToArray());
			this.textBoxLevel.Text = text;
		}

		private void hyTextBox1_DropDown(object sender, EventArgs e)
		{
			string text = this.hyTextBox1.Text;
			string value = "";
			if (!this.IsItemsContainsStr(this.hyTextBox1.Items, text, ref value))
			{
				List<string> list = new List<string>();
				foreach (object obj in this.hyTextBox1.Items)
				{
					list.Add(obj.ToString());
				}
				if (list.Count == 10)
				{
					list.Remove(list.Last<string>());
				}
				this.hyTextBox1.Items.Clear();
				this.hyTextBox1.Items.Add(text);
				this.hyTextBox1.Items.AddRange(list.ToArray());
				return;
			}
			this.hyTextBox1.Items.Remove(value);
			List<string> list2 = new List<string>();
			foreach (object obj2 in this.hyTextBox1.Items)
			{
				list2.Add(obj2.ToString());
			}
			this.hyTextBox1.Items.Clear();
			this.hyTextBox1.Items.Add(text);
			this.hyTextBox1.Items.AddRange(list2.ToArray());
			this.hyTextBox1.Text = text;
		}

		private void textBoxOffset_DropDown(object sender, EventArgs e)
		{
			string text = this.textBoxOffset.Text;
			string value = "";
			if (!this.IsItemsContainsStr(this.textBoxOffset.Items, text, ref value))
			{
				List<string> list = new List<string>();
				foreach (object obj in this.textBoxOffset.Items)
				{
					list.Add(obj.ToString());
				}
				if (list.Count == 10)
				{
					list.Remove(list.Last<string>());
				}
				this.textBoxOffset.Items.Clear();
				this.textBoxOffset.Items.Add(text);
				this.textBoxOffset.Items.AddRange(list.ToArray());
				return;
			}
			this.textBoxOffset.Items.Remove(value);
			List<string> list2 = new List<string>();
			foreach (object obj2 in this.textBoxOffset.Items)
			{
				list2.Add(obj2.ToString());
			}
			this.textBoxOffset.Items.Clear();
			this.textBoxOffset.Items.Add(text);
			this.textBoxOffset.Items.AddRange(list2.ToArray());
			this.textBoxOffset.Text = text;
		}

		private void btnslope_Click(object sender, EventArgs e)
		{
			double num;
			if (double.TryParse(this.hyTextBox1.Text, out num))
			{
				if (num > 0.0)
				{
					this.hyTextBox1.Text = "-" + this.hyTextBox1.Text;
					return;
				}
				if (num < 0.0)
				{
					this.hyTextBox1.Text = this.hyTextBox1.Text.Substring(1, this.hyTextBox1.Text.Length - 1);
					return;
				}
			}
			else
			{
				YJKMessageBox.Show("输入数字格式不正确，不能进行正负变换！");
				this.hyTextBox1.Focus();
				this.hyTextBox1.SelectAll();
			}
		}

		private void btnOffset_Click(object sender, EventArgs e)
		{
			double num;
			if (double.TryParse(this.textBoxOffset.Text, out num))
			{
				if (num > 0.0)
				{
					this.textBoxOffset.Text = "-" + this.textBoxOffset.Text;
					return;
				}
				if (num < 0.0)
				{
					this.textBoxOffset.Text = this.textBoxOffset.Text.Substring(1, this.textBoxOffset.Text.Length - 1);
					return;
				}
			}
			else
			{
				YJKMessageBox.Show("输入数字格式不正确，不能进行正负变换！");
				this.textBoxOffset.Focus();
				this.textBoxOffset.SelectAll();
			}
		}

		private Document doc;

		private List<string> levelUp = new List<string>();

		public double level;

		public double Offset;

		public double slope;

		public PipeType pipeTypeRiser;

		public PipingSystemType pipingSystemTypeRiser;

		public string diameter = "0";

		private HVACUserSetting userSetting = new HVACUserSetting();

		private OperateSegmentFitting osf;

		public HYHeatingPipeSystemType systemType;

		private bool isDra = true;

		private string textDiameter = "100";

		private string textLevel = "1.0";

		private string textOffset = "0";
	}
}
